package com.smart.ve.util;

import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class Mail {

	 public static void main(String[] args)
	 {
		 try {
			 System.out.println(getInstance().sendMail("mail", ""));
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }
	
	private static Mail instance = new Mail();

    public static Mail getInstance() {
    	//System.out.println("Mail getInstance");
        return instance;
    }

    private Mail() {
    }

    private class SMTPAuthenticator extends Authenticator {
    	PasswordAuthentication passwordAuthentication;
    	SMTPAuthenticator(String userName, String password) {
    		passwordAuthentication = new PasswordAuthentication(userName, password);
    	}
		public PasswordAuthentication getPasswordAuthentication() {
			return passwordAuthentication;
		}
    }

    /**
     * 占쏙옙占쏙옙 占쏙옙占쏙옙占쏙옙

     * @return
     * @throws Exception
     */
    public String sendMail(String toMail, String userName) throws Exception {
    	// name     : 蹂대궪�щ엺 �대쫫
        // from     : 蹂대궪�щ엺 二쇱냼
        // to       : 諛쏆쓣�щ엺 二쇱냼
        // title    : �쒕ぉ
        // content  : �댁슜
        // tar      : text or html
        // filename ; 泥⑤��뚯씪 �덈� 寃쎈줈(�뚯씪紐��ы븿)

    	String host = "smtp.naver.com";
        String usid = "lalunabella";
        String pswd = "khj20540";
        
        String from = "lalunabella@naver.com";
        String to = toMail;
        
        String content="";
        
        content += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
     	content += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
        content += "<head>";
        content += "<title>Smart VE 媛�엯 �몄쬆硫붿씪</title>";
        content += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
        content += "</head>";
        content += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
        content += "<table width=\"530\" height=\"550\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\" style=\"border-collapse:collapse;\">";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_01.jpg\" width=\"530\" height=\"175\" alt=\"Smart VE �뚯썝媛�엯 �몄쬆硫붿씪\">";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td width=\"75\" height=\"100\"></td>";
        content += "		<td width=\"380\" height=\"100\" leftmargin=\"75\" style=\"font-size:12px; line-height:1.6;\">";
        content += "			�덈뀞�섏꽭�� " + userName + " ��!<br />";
        content += "			Smart VE 怨꾩젙 �깅줉���붿껌�섏뀲�듬땲��<br />";
        content += "			�뚯썝媛�엯 諛��쒖꽦�붾� �꾪빐�쒕뒗 �대찓���몄쬆���꾩슂�⑸땲��<br />";
        content += "			�꾨옒 '媛�엯�꾨즺' 踰꾪듉���대┃��二쇱꽭��";
        content += "		</td>";
        content += "		<td width=\"75\" height=\"100\"></td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"180\">";
        //content += "			<a href=\"http://localhost:8080/smartve/mailok.do?action=mailok&id="+toMail+"\"><img src=\"images/auth_04.jpg\" width=\"205\" height=\"50\" alt=\"媛�엯�꾨즺\"></a>";
        content += "			<a href=\"http://121.78.122.210:8080/smartve/mailok.do?action=mailok&id="+toMail+"\"><img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_04.jpg\" width=\"205\" height=\"50\" alt=\"媛�엯�꾨즺\"></a>";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_07.jpg\" width=\"530\" height=\"95\" alt=\"SMART VE\">";
        content += "		</td>";
        content += "	</tr>";
        content += "</table>";
        content += "</body>";
        content += "</html>";
        
        
        
        String title = "[PMSC Smart VE] �덈뀞�섏꽭�� " + userName +"(" + toMail + ") �섏쓽 SMART VE �뚯썝媛�엯�� �섏쁺 �⑸땲��";

    	try{
	    	Properties props = new Properties();
	    	props.put("mail.transport.protocol", "smtp");
	    	props.put("mail.smtp.starttls.enable","true");
	    	props.put("mail.smtp.host", host);
	    	props.put("mail.smtp.auth", "true");
	    	Authenticator authenticator = new SMTPAuthenticator(usid, pswd);

	    	// use mail address from HTML form for from address
	    	props.put("mail.from", from);
	    	Session session = Session.getInstance(props, authenticator);

	    	Message msg = new MimeMessage(session);
	    	msg.setFrom(new InternetAddress(from));
	    	msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
	    	msg.setSubject(title);
	    	msg.setSentDate(new Date());
	    	
	    	// Content is stored in a MIME multi-part message
	    	// with one body part
	    	MimeBodyPart mbp = new MimeBodyPart();
	    	//mbp.setText(content);
	    	mbp.setContent(content, "text/html;charset=utf-8");
	    	Multipart mp = new MimeMultipart();
	    	mp.addBodyPart(mbp);
	    	msg.setContent(mp, "text/html;charset=utf-8");

	    	Transport.send(msg);


        }
        catch(Exception e){
        	e.printStackTrace();
        }

    	return "succ";
    }
    
    public String sendPassMail(String toMail, String userName) throws Exception {
    	// name     : 蹂대궪�щ엺 �대쫫
        // from     : 蹂대궪�щ엺 二쇱냼
        // to       : 諛쏆쓣�щ엺 二쇱냼
        // title    : �쒕ぉ
        // content  : �댁슜
        // tar      : text or html
        // filename ; 泥⑤��뚯씪 �덈� 寃쎈줈(�뚯씪紐��ы븿)

    	String host = "smtp.cafe24.com";
        String usid = "ve@pmsc.co.kr";
        String pswd = "pms0070";
        
        String from = "ve@pmsc.co.kr";
        String to = toMail;
        
        String content="";
        
        content += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
     	content += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
        content += "<head>";
        content += "<title>Smart VE 鍮꾨�踰덊샇 �ъ꽕��/title>";
        content += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
        content += "</head>";
        content += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
        content += "<table width=\"530\" height=\"570\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\" style=\"border-collapse:collapse;\">";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/pw_reset_top.jpg\" width=\"530\" height=\"160\" alt=\"Smart VE 鍮꾨�踰덊샇 �ъ꽕�뺤씠 �꾨즺�섏뿀�듬땲��\">";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td width=\"70\" height=\"100\"></td>";
        content += "		<td width=\"390\" height=\"100\" style=\"font-size:12px; line-height:1.6; vertical-align:top;\">";
        content += "			" + userName +"(" + toMail + ") <br /> ��怨꾩젙 鍮꾨�踰덊샇瑜��ъ꽕���섏떆�ㅻ㈃ <br /> �꾨옒��踰꾪듉��";
        content += "			�대┃��二쇱꽭��<br /> 留뚯빟 鍮꾨�踰덊샇 �ъ꽕�뺤쓣 �먯튂 �딆쑝�좊떎硫�<br />��硫붿씪��";
        content += "			臾댁떆�섏뀛��醫뗭뒿�덈떎.";
        content += "		</td>";
        content += "		<td width=\"70\" height=\"100\"></td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"50\">";
        //content += "			<a href=\"http://localhost:8080/smartve/change.do?action=change&id="+toMail+"\"><img src=\"images/pw_reset_btn.jpg\" width=\"205\" height=\"50\" alt=\"鍮꾨�踰덊샇�ъ꽕��"></a>";
        content += "			<a href=\"http://121.78.122.210:8080/smartve/change.do?action=change&id="+toMail+"\"><img src=\"images/pw_reset_btn.jpg\" width=\"205\" height=\"50\" alt=\"鍮꾨�踰덊샇�ъ꽕��\"></a>";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"35\">";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td width=\"70\" height=\"58\" ";
        content += "		</td>";
        content += "		<td width=\"390\" height=\"100\" style=\"font-size:12px; line-height:1.6; vertical-align:top;\">";
        content += "		�꾩쓽 踰꾪듉���묐룞�섏� �딅뒗�ㅻ㈃ �꾨옒 URL��蹂듭궗�댁꽌 二쇱냼李쎌뿉<br />";
        content += "		遺숈뿬 �ｊ린 �섏꽭��<br />";
        content += "		</td>";
        content += "		<td width=\"70\" height=\"50\" ";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td width=\"70\" height=\"58\" ";
        content += "		</td>";
        content += "		<td width=\"390\" height=\"58\" align=\"center\" background=\"http://121.78.122.210:8080/smartve/resources/images/pw_reset_bg.jpg\" style=\"font-size:12px; line-height:1.6;\">";
//        content += "		http://localhost:8080/smartve/change.do?action=change&id="+toMail+"" ;
        content += "		http://121.78.122.210:8080/smartve/change.do?action=change&id="+toMail+"" ;
        content += "		</td>";
        content += "		<td width=\"70\" height=\"58\" ";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"22\">";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_07.jpg\" width=\"530\" height=\"95\" alt=\"Smart VE\">";
        content += "		</td>";
        content += " 	</tr>";
        content += "</table>";
        content += "</body>";
        content += "</html>";
        
        
        
        String title = "[PMSC Smart VE] �덈뀞�섏꽭�� " + userName +"(" + toMail + ") �섏쓽 SMART VE 鍮꾨�踰덊샇 �ъ꽕���⑸땲��";

    	try{
	    	Properties props = new Properties();
	    	props.put("mail.transport.protocol", "smtp");
	    	props.put("mail.smtp.starttls.enable","true");
	    	props.put("mail.smtp.host", host);
	    	props.put("mail.smtp.auth", "true");
	    	Authenticator authenticator = new SMTPAuthenticator(usid, pswd);

	    	// use mail address from HTML form for from address
	    	props.put("mail.from", from);
	    	Session session = Session.getInstance(props, authenticator);

	    	Message msg = new MimeMessage(session);
	    	msg.setFrom(new InternetAddress(from));
	    	msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
	    	msg.setSubject(title);
	    	msg.setSentDate(new Date());
	    	
	    	// Content is stored in a MIME multi-part message
	    	// with one body part
	    	MimeBodyPart mbp = new MimeBodyPart();
	    	//mbp.setText(content);
	    	mbp.setContent(content, "text/html;charset=utf-8");
	    	Multipart mp = new MimeMultipart();
	    	mp.addBodyPart(mbp);
	    	msg.setContent(mp, "text/html;charset=utf-8");

	    	Transport.send(msg);


        }
        catch(Exception e){
        	e.printStackTrace();
        }

    	return "succ";
    }
    
    public String sendExcel(String toMail, String idx) throws Exception {


    	String host = "smtp.cafe24.com";
        String usid = "ve@pmsc.co.kr";
        String pswd = "pms0070";
        
        String from = "ve@pmsc.co.kr";
        String to = toMail;
        
        String content="";
        
        content += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
     	content += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
        content += "<head>";
        content += "<title>Smart VE 湲곕뒫紐⑸줉</title>";
        content += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
        content += "</head>";
        content += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
        content += "<table width=\"530\" height=\"550\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\" style=\"border-collapse:collapse;\">";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "		</td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td width=\"75\" height=\"100\"></td>";
        content += "		<td width=\"380\" height=\"100\" leftmargin=\"75\" style=\"font-size:12px; line-height:1.6;\">";

        content += "			�꾨옒 '�묒��먮즺諛쏄린' �띿뒪�몃� �대┃��二쇱꽭��";
        content += "		</td>";
        content += "		<td width=\"75\" height=\"100\"></td>";
        content += "	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\" align=\"center\" width=\"530\" height=\"180\">";
        //content += "			<a href=\"http://localhost:8080/smartve/mailok.do?action=mailok&id="+toMail+"\"><img src=\"images/auth_04.jpg\" width=\"205\" height=\"50\" alt=\"媛�엯�꾨즺\"></a>";
        content += "			<a href=\"http://121.78.122.210:8080/smartve/excelDown.do?idx="+idx+"\">�묒��먮즺諛쏄린</a>";
        content += "		</td>";
        content += " 	</tr>";
        content += "	<tr>";
        content += "		<td colspan=\"3\">";
        content += "			<img src=\"http://121.78.122.210:8080/smartve/resources/images/auth_07.jpg\" width=\"530\" height=\"95\" alt=\"SMART VE\">";
        content += "		</td>";
        content += "	</tr>";
        content += "</table>";
        content += "</body>";
        content += "</html>";
        
        
        
        String title = "[PMSC Smart VE] 湲곕뒫紐⑸줉�낅땲��";

    	try{
	    	Properties props = new Properties();
	    	props.put("mail.transport.protocol", "smtp");
	    	props.put("mail.smtp.starttls.enable","true");
	    	props.put("mail.smtp.host", host);
	    	props.put("mail.smtp.auth", "true");
	    	Authenticator authenticator = new SMTPAuthenticator(usid, pswd);

	    	// use mail address from HTML form for from address
	    	props.put("mail.from", from);
	    	Session session = Session.getInstance(props, authenticator);

	    	Message msg = new MimeMessage(session);
	    	msg.setFrom(new InternetAddress(from));
	    	msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
	    	msg.setSubject(title);
	    	msg.setSentDate(new Date());
	    	
	    	// Content is stored in a MIME multi-part message
	    	// with one body part
	    	MimeBodyPart mbp = new MimeBodyPart();
	    	//mbp.setText(content);
	    	mbp.setContent(content, "text/html;charset=utf-8");
	    	Multipart mp = new MimeMultipart();
	    	mp.addBodyPart(mbp);
	    	msg.setContent(mp, "text/html;charset=utf-8");

	    	Transport.send(msg);


        }
        catch(Exception e){
        	e.printStackTrace();
        }

    	return "succ";
    }

}
